package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.dao.EnterpriseEvaluateDao;
import io.renren.modules.generator.dao.EnterpriseMainDao;
import io.renren.modules.generator.entity.EnterpriseEvaluateEntity;
import io.renren.modules.generator.service.EnterpriseEvaluateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


@Service("enterpriseEvaluateService")
public class EnterpriseEvaluateServiceImpl extends ServiceImpl<EnterpriseEvaluateDao, EnterpriseEvaluateEntity> implements EnterpriseEvaluateService {

    @Autowired
    private EnterpriseMainDao enterpriseMainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,EnterpriseEvaluateEntity enterpriseEvaluateEntity) {
        QueryWrapper<EnterpriseEvaluateEntity> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(enterpriseEvaluateEntity.getDeptId())){
            List<String> list = enterpriseMainDao.getEnterpriseList(enterpriseEvaluateEntity.getDeptId(), null);
            if (!CollectionUtils.isEmpty(list)) {
                queryWrapper.in("enterprise_id", list);
            }
        }
        queryWrapper.orderByDesc("create_time");
        IPage<EnterpriseEvaluateEntity> page = this.page(
                new Query<EnterpriseEvaluateEntity>().getPage(params),
                queryWrapper
        );
        page.getRecords().forEach(enterpriseEvaluateEntity1 -> {
            enterpriseEvaluateEntity1.setEnterpriseName(
                    enterpriseMainDao.selectById(enterpriseEvaluateEntity1.getEnterpriseId()).getName()
            );
        });
        return new PageUtils(page);
    }

}
